package com.starbucks.opencase.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.data.daPath;
import com.starbucks.opencase.function.funOnHook;
import com.starbucks.opencase.modualr.moGoodsSummary;
import com.starbucks.opencase.modualr.moPetSummary;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMMark;
import com.starbucks.opencase.function.funCarry;
import com.starbucks.opencase.function.funSession;
import com.starbucks.opencase.function.funUserTest;
import com.starbucks.opencase.tool.toolUserGoods;
import com.starbucks.opencase.tool.toolUserPet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class serUserBag {
    @Autowired
    private funUserTest funUserTest;
    @Autowired
    private funSession funSession;
    @Autowired
    private toolUserPet toolUserPet;
    @Autowired
    private toolUserGoods toolUserGoods;
    @Autowired
    private funCarry funCarry;
    @Autowired
    private funOnHook funOnHook;
    /**
     * 查询活跃的宠物
     * @param id
     * @param message
     */
    public void queryActivePet(String id, JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.queryActivePet.ordinal());
        on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
        if(!funSession.isOnlineOfId(id,on))return;
        String uid= funSession.getUIdOfId(id);
        ArrayNode an=om.createArrayNode();
        List<moPetSummary> ps=toolUserPet.queryActive(uid);
        for(moPetSummary p:ps){
            p.setUrl(daPath.getPet(p.getPid()));
            if(funCarry.isCarry(uid,p.getPid()))p.setState(1);
            else if(funOnHook.isExit(uid,p.getPid()))p.setState(2);
            p.setAnimationJson(daPath.getAnimationJson(p.getPid()));
            p.setAnimationAtlas(daPath.getAnimationAtlas(p.getPid()));
            p.setAnimationPng(daPath.getAnimationPng(p.getPid()));
            an.add(om.valueToTree(p));
        }
        on.put(enumCMKey.list.name(), an);
        on.put(enumCMKey.status.name(), true);
        funSession.sendMessage(id,on);
    }

    /**
     * 查询背包
     * @param id
     * @param message
     */
    public void queryBag(String id,JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.queryBag.ordinal());
        on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
        if(!funSession.isOnlineOfId(id,on))return;
        String uid=funSession.getUIdOfId(id);
        List<moGoodsSummary> ls=toolUserGoods.queryGoods(uid);
        ArrayNode an=om.createArrayNode();
        for(moGoodsSummary mo:ls){
            mo.setUrl(daPath.getGoods(mo.getGid()));
            an.add(om.valueToTree(mo));
        }
        on.put(enumCMKey.list.name(), an);
        on.put(enumCMKey.status.name(), true);
        funSession.sendMessage(id,on);
    }
}
